package cn.cqie.attendance.teacher.service.impl;

import cn.cqie.attendance.common.util.DateUtil;
import cn.cqie.attendance.common.util.LocalJwt;
import cn.cqie.attendance.common.util.Page;
import cn.cqie.attendance.common.util.PageBean;
import cn.cqie.attendance.teacher.dao.TeacherLeaveDao;
import cn.cqie.attendance.teacher.pojo.TeacherLeave;
import cn.cqie.attendance.teacher.pojo.TeacherLeaveLog;
import cn.cqie.attendance.teacher.service.TeacherLeaveService;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.http.HttpServletRequest;
import java.text.ParseException;
import java.util.*;

/**
 * @author wqh
 * @date 2021/6/21 17:15
 */
@Service
public class TeacherLeaveServiceImpl implements TeacherLeaveService {

    @Autowired
    private TeacherLeaveDao teacherLeaveDao;
    @Autowired
    private HttpServletRequest request;
    //当前登录教师id
    //int teacherId = 1;
    @Override
    public PageBean<TeacherLeave> queryLeaveByTeacherId(Date beginTime,Date endTime, Integer collegeId, Integer majorId, Integer gradeId, Integer classId, String stuNumber, String stuName, String leaveTheme, Integer leaveReason, Integer leaveStatus,Integer pageNum,Integer pageSize) {
        int teacherId = LocalJwt.getCurrentUserId(request);
        if (pageNum == null || pageSize == null){
            pageNum = 1;
            pageSize = 10;
        }
        PageBean pageBean = new PageBean();
        pageBean.setPageNum(pageNum);
        pageBean.setPageSize(pageSize);
        PageHelper.startPage(pageBean.getPageNum(),pageBean.getPageSize());
        Map<String,Object> map = new HashMap<>();
        map.put("teacherId",teacherId);
        map.put("beginTime",beginTime);
        map.put("endTime",endTime);
        map.put("collegeId",collegeId);
        map.put("majorId",majorId);
        map.put("gradeId",gradeId);
        map.put("classId",classId);
        map.put("stuNumber",stuNumber);
        map.put("stuName",stuName);
        map.put("leaveTheme",leaveTheme);
        map.put("leaveReason",leaveReason);
        map.put("leaveStatus",leaveStatus);
        map.put("pageNum",pageNum);
        map.put("pageSize",pageSize);
        List<TeacherLeave> teacherLeaveList = teacherLeaveDao.queryLeaveByTeacherId(map);
        int total = teacherLeaveList.size();

        if (null == teacherLeaveList || total == 0) {
            return new PageBean<>(500, "数据为空！");
        }
        return new PageBean<>(200, "Success", teacherLeaveList);
    }

    @Override
    public Page queryLeaveInfoByLeaveId(Integer leaveId) {
        List<TeacherLeave> teacherLeaveList = teacherLeaveDao.queryLeaveInfoByLeaveId(leaveId);
        return new Page(200,"success",teacherLeaveList);
    }

    @Override
    public PageBean<TeacherLeaveLog> queryLeaveLogInfoByLeaveLogId(Integer leaveId,Integer pageNum,Integer pageSize) {
        if (pageNum == null || pageSize == null){
            pageNum = 1;
            pageSize = 10;
        }
        PageBean pageBean = new PageBean();
        pageBean.setPageNum(pageNum);
        pageBean.setPageSize(pageSize);
        PageHelper.startPage(pageBean.getPageNum(),pageBean.getPageSize());
        List<TeacherLeaveLog> teacherLeaveLogs = teacherLeaveDao.queryLeaveLogInfoByLeaveLogId(leaveId);
        int total = teacherLeaveLogs.size();

        if (null == teacherLeaveLogs || total == 0) {
            return new PageBean<>(500, "数据为空！");
        }
        return new PageBean<>(200, "Success",teacherLeaveLogs);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Page updateLeaveStatusByLeaveId(String[] leaveIds,Integer headmaster) throws ParseException {
        int teacherId = LocalJwt.getCurrentUserId(request);
        //审批时间
        Date transitTime =  DateUtil.formatDate("yyyy-MM-dd HH:mm:ss",new Date());
        //判断是否是班主任
        if (teacherId == headmaster){
            Map<String,Object> map = new HashMap<>();
            map.put("leaveIds",leaveIds);
            map.put("transitTime",transitTime);
            int count = teacherLeaveDao.updateLeaveStatusByLeaveId(map);
            if (count > 0){
                return Page.SUCCESS;
            }else {
                return Page.FAIL;
            }
        }else {
            return new Page(201,"当前用户没有该权限");
        }
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Page updateLeaveByLeaveId(Integer leaveId, String failedDetail,String reasonDetail, Integer headmaster,Integer stuId) throws ParseException {
        int teacherId = LocalJwt.getCurrentUserId(request);
        //审批时间
        Date transitTime =  DateUtil.formatDate("yyyy-MM-dd HH:mm:ss",new Date());
        //判断是否是班主任
        if (teacherId == headmaster){
            Map<String,Object> map = new HashMap<>();
            map.put("leaveId",leaveId);
            map.put("transitTime",transitTime);
            int count = teacherLeaveDao.updateLeaveByLeaveId(map);
            if (count > 0){
                Map<String,Object> mapLog = new HashMap<>();
                mapLog.put("teacherId",teacherId);
                mapLog.put("stuId",stuId);
                mapLog.put("leaveId",leaveId);
                mapLog.put("leaveStatus",0);
                mapLog.put("transitTime",transitTime);
                mapLog.put("reasonDetail",reasonDetail);
                mapLog.put("failedDetail",failedDetail);
                teacherLeaveDao.insertIntoLeaveLog(mapLog);
                return Page.SUCCESS;
            }else {
                return Page.FAIL;
            }
        }else {
            return new Page(201,"当前用户没有该权限");
        }
    }

    /*@Override
    public Page insertIntoLeaveLog(Integer leaveId, Integer leaveStatus, String reasonDetail, String failedDetail) throws ParseException {
        //审批时间
        Date transitTime =  DateUtil.formatDate("yyyy-MM-dd HH:mm:ss",new Date());
        Map<String,Object> map = new HashMap<>();
        map.put("teacherId",teacherId);
        map.put("leaveId",leaveId);
        map.put("leaveStatus",leaveStatus);
        map.put("transitTime",transitTime);
        map.put("reasonDetail",reasonDetail);
        map.put("failedDetail",failedDetail);
        int count = teacherLeaveDao.insertIntoLeaveLog(map);
        if (count > 0){
            return Page.SUCCESS;
        }else {
            return Page.FAIL;
        }
    }*/

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Page deleteLeaveByLeaveId(String leaveIds,Integer headmaster) {
        int teacherId = LocalJwt.getCurrentUserId(request);
        if (teacherId == headmaster){
            String[] leaveId = leaveIds.split(",");
            int count = teacherLeaveDao.deleteLeaveByLeaveId(leaveId);
            if (count > 0){
                int countL = teacherLeaveDao.deleteLeaveLogByLeaveLogId(leaveId);
                if (countL > 0){
                    return Page.SUCCESS;
                }else {
                    return Page.FAIL;
                }
            }else {
                return Page.FAIL;
            }
        }else {
            return new Page(201,"当前用户没有该权限");
        }
    }
/*    @Override
    public Page deleteLeaveLogByLeaveLogId(String leaveLogIds) {
        String[] leaveLogId = leaveLogIds.split(",");
        int count = teacherLeaveDao.deleteLeaveLogByLeaveLogId(leaveLogId);
        if (count > 0){
            return Page.SUCCESS;
        }else {
            return Page.FAIL;
        }
    }*/
}
